package com.example.a2306_shop.minefg

import androidx.lifecycle.viewModelScope
import com.example.lib_base.entity.OrderEntity
import com.example.lib_base.entity.Res
import com.example.lib_base.mvvm.BaseViewModel
import com.example.lib_base.param.Comment
import com.example.lib_base.param.Refund
import com.example.lib_base.utils.SingleLiveData
import kotlinx.coroutines.launch

class OrderViewModel:BaseViewModel() {
    //订单列表
    val orderSuccess = SingleLiveData<Res<OrderEntity>>()
    val orderFail = SingleLiveData<String>()
    fun order(type:String,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                orderSuccess.postValue(apiService.order(type, limit, page))
            } catch (e: Exception) {
                orderFail.postValue(e.message.toString())
            }
        }
    }
    //评价订单
    val sendSuccess = SingleLiveData<Res<Boolean>>()
    val sendFail = SingleLiveData<String>()
    fun commentSend(comment: Comment){
        viewModelScope.launch {
            try {
                sendSuccess.postValue(apiService.commentSend(comment))
            } catch (e: Exception) {
                sendFail.postValue(e.message.toString())
            }
        }
    }
    //删除订单
    val deleteSuccess = SingleLiveData<Res<String?>>()
    val deleteFail = SingleLiveData<String>()
    fun orderDelete(id:Int){
        viewModelScope.launch {
            try {
                deleteSuccess.postValue(apiService.orderDelete(id))
            } catch (e: Exception) {
                deleteFail.postValue(e.message.toString())
            }
        }
    }
    //申请退款
    val refundSuccess = SingleLiveData<Res<Boolean>>()
    val refundFail = SingleLiveData<String>()
    fun orderRefund(refund: Refund){
        viewModelScope.launch {
            try {
                refundSuccess.postValue(apiService.orderRefund(refund))
            } catch (e: Exception) {
                refundFail.postValue(e.message.toString())
            }
        }
    }
}